﻿using System.ServiceProcess;

namespace CokeeAgent
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new CapService()
            };
            ServiceBase.Run(ServicesToRun);
            if (args != null)
            {
                if (args.Contains("-console"))
                {
                    var a = new CapService();
                    a.isCls = true;
                    a.Start();
                    Console.ReadLine();
                }
            }
        }

        public static void WriteInfo(string info)
        {
            var dir = @"D:\logs\v2\";
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
            using (FileStream stream = new FileStream(dir + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine($"{DateTime.Now} | {info}");
                }
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            WriteInfo(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}\r\n异常堆栈：{2}", ex.GetType(), ex.Message, ex.StackTrace));
        }
    }
}